Use case: Encoder interface in interrupt mode

HAL1

HAL2

Initialization and configuration

  • The function HAL_TIM_Encoder_Init(&htim1, &sConfig) is used to configure:

    • The TIMx peripheral.

    • Encoder mode.

    • The TIMx CH1 and CH2.

Start encoder interface in interrupt mode

  • The function HAL_TIM_Encoder_Start_IT is used to start the TIM Encoder Interface in interrupt mode.

TIMx IRQ handler

  • Inside the interrupt handler for TIMx, the user calls the global IRQ handler of the TIM driver HAL_TIM_IRQHandler() to handle the capture compare x interrupt request.

Callback function

  • The function HAL_TIM_IC_CaptureCallback() is called when the input capture occurs.

Initialization and configuration

  • The functions HAL_TIM_Init and HAL_TIM_SetConfig are used to configure:

    • The TIMx peripheral.

    • Encoder mode.

  • The functions HAL_TIM_IC_SetConfigChannel and HAL_TIM_IC_SetConfigCaptureUnit are used to configure TIMx channels (CH1 and CH2).

Start encoder interface in interrupt mode

  • The functions HAL_TIM_IC_StartChannel_IT and HAL_TIM_Start() are used to start the encoder interface in interrupt mode.

TIMx IRQ handler

  • Inside the interrupt handler for TIMx, the user can call either:

    • The global IRQ handler HAL_TIM_IRQHandler() to handle the capture compare x interrupt request, or

    • The single process IRQ handler for specific interrupt events in this case HAL_TIM_CC_IRQHandler().

Callback function

  • The function HAL_TIM_InputCaptureCallback() is called when the input capture event occurs.

Sequence diagram

Encoder interface

@startuml
title "Encoder Mode in HAL 1"

participant "User Application" as App
participant "HAL TIM" as Driver
participant "MSP" as MSP

group Initialization and Configuration
    App -> Driver: <b><font color=purple>HAL_TIM_Encoder_Init</b>(&(TIM_Encoder_InitTypeDef){\nSet Instance (TIMx)\nSet Init structure\nEncoderMode = TIM_ENCODERMODE_TI12,\nIC1Polarity = TIM_ICPOLARITY_RISING,\nIC1Selection = TIM_ICSELECTION_DIRECTTI,\nIC1Prescaler = TIM_ICPSC_DIV1,\nIC1Filter = 0,\nIC2Polarity = TIM_ICPOLARITY_RISING,\nIC2Selection = TIM_ICSELECTION_DIRECTTI,\nIC2Prescaler = TIM_ICPSC_DIV1,\nIC2Filter = 0})
    Driver -> MSP: HAL_TIM_ENCODER_MspInit()
    MSP -> Driver: Enable TIMx clock\nCH1, CH2 GPIO AF config
end

group Start TIMx in IT Mode
    App -> Driver: <b><font color=green>HAL_TIM_Encoder_Start_IT</b>(TIM_CHANNEL_ALL)
    Driver->Driver : Enable capture compare interrupt\nEnable TIMx counter
    Driver --> App : HAL_OK
    IT <[#red]-] : <font color=red><b>TIMx IRQ
    IT -> Driver : HAL_TIM_IRQHandler()
    Driver -> Driver : Clear flag
    Driver -> App: HAL_TIM_IC_CaptureCallback()
    App --> Driver
    Driver --> IT
    end
@enduml

Code snippet

HAL1

HAL2

/* Declaration of tim handle */
TIM_HandleTypeDef htim3;

/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting encoder mode.*/
.............

/* Start the encoder mode in interrupt */
HAL_TIM_Encoder_Start_IT(&htim3, TIM_CHANNEL_ALL);

/* User must call the Interrupt Handler
  functions in TIM3 global interrupt */

void TIM3_IRQHandler(void)
{
    /* Code to execute at each interrupt */
    HAL_TIM_IRQHandler(&htim3);
}

/* Call the callback function */


void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
      /* Get the Input Capture value */
      uwIC2Value = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2);
}
 /* Declaration of the tim handle */
 hal_tim_handle_t htim3;

 /* Ensure that all required peripherals
 (TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
 before starting encoder mode.*/
      .............

/* Start the encoder mode in interrupt */
 HAL_TIM_IC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_1);
 HAL_TIM_IC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_2);
 HAL_TIM_Start(&htim3);

 /* User must call the Interrupt Handler
  functions in TIM2 global interrupt */

 void TIM3_IRQHandler(void)
 {
     /* Code to execute at each interrupt */
     HAL_TIM_IRQHandler(&htim3);
 }

 /* Call the callback function */

 void HAL_TIM_InputCaptureCallback(hal_tim_handle_t *htim,
                          hal_tim_channel_t channel);
 {
     /* Get the Input Capture value */
     uwIC2Value = HAL_TIM_IC_ReadChannelCapturedValue(&htim3, HAL_TIM_CHANNEL_2)
 }

Key changes

  1. Encoder initialization is unified in HAL2 using HAL_TIM_Init() + HAL_TIM_SetConfig() and channel configuration uses input-capture configuration APIs.

  2. Encoder start in interrupt mode is performed with per-channel start APIs and requires HAL_TIM_Start().